package bit.lotterysystem.dao.mapper;


import bit.lotterysystem.dao.dataobject.Encrypt;
import bit.lotterysystem.dao.dataobject.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    /**
     * 查询邮箱绑定的人数
     * @param email
     * @return
     */
    @Select("select count(*) from user where email=#{email}")
    int countByMail(@Param("email") String email);

    @Select("select count(*) from user where phone_number=#{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    @Insert("insert into user (user_name,email,phone_number,password,identity)"
    + " values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    /**
     * keyProkperty:指定把数据库产生的值映射到JAVA对象中的哪个属性
     * keyColuumn:当数据表字段名与JAVA属性不一致时，直接指定数据库表中主键字段的名称（假如相同即可省略）
     */
    void insert(UserDO userDO);

    @Select("select * from user where email=#{email}")
    UserDO selectByMail(@Param("email") String email);

    @Select("select * from user where phone_number=#{phoneNumber}")
    UserDO selectByPhoneNumber( @Param("phoneNumber") Encrypt phoneNumber);

    @Select("<script> select * from user " +
            "<if test=\"identity!=null\">" +
            " where identity=#{identity} </if> " +
            "order by id desc</script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identity);

    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='list' open='(' separator=',' close=')'>" +
            " #{item}"+
            " </foreach>"+
            " </script>")
    List<Long> selectExistByIds(@Param("list") List<Long> id);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection='list' open='(' separator=',' close=')'>" +
            " #{item}"+
            " </foreach>"+
            " </script>")
    List<UserDO> batchSelectByIds(@Param("list") List<Long> ids);
}
